%% Macros needed for EMaxima.
%% Jay Belanger (belanger@truman.edu)
%%
%% Some of this was taken from Dan Dill's TeX/MMA (in modified
%% form, of course), and some was taken from other sources.  Some
%% I may even have written myself.

%% Here are the values that the user may wish to change.
%% They can be changed here or in the main TeX file.

%% For normal cells
\newif\ifmaximalines
\maximalinestrue
\newdimen\premaximaspace
\premaximaspace = 0pt
\newdimen\postmaximaspace
\postmaximaspace = 1.5ex

%% For the input
\font\maximainputfont = cmtt10
\newdimen\maximainputbaselineskip
\maximainputbaselineskip = \baselineskip %10pt

%% For the output
\font\maximaoutputfont = cmtt10
\newdimen\maximaoutputbaselineskip
\maximaoutputbaselineskip = \baselineskip %14pt

%% For session cells
\newif\ifmaximasessionlines
\maximasessionlinesfalse
\newdimen\premaximasessionspace
\premaximasessionspace = 0pt
\newdimen\postmaximasessionspace
\postmaximasessionspace = 1.5ex

%% For a normal session
\font\maximasessionoutputfont = cmtt10
\newdimen\maximasessionbaselineskip
\maximasessionbaselineskip = \baselineskip %16pt

%% For a TeX session
\newdimen\maximatexsessionbaselineskip
\maximatexsessionbaselineskip = \baselineskip %16pt
\def\maximapromptcolor{red}
\def\maximainputcolor{blue}
\def\maximaoutputcolor{blue}
\def\maximasessionnocolor{
  \def\maximapromptcolor{black}
  \def\maximainputcolor{black}
  \def\maximaoutputcolor{black}
}

%% Some things are handled differently when pdflatex is used.

\ifx\pdfoutput\undefined
  \usepackage{color}
\else
  \usepackage[pdftex]{color}
  \usepackage{pdfcolmk}
  \color{black}
\fi

%% A macro for writing Maxima in the ``proper'' font

\def\Maxima{{\sffamily\slshape Maxima\/}}
\def\maximaheadersize{\tiny}

%% Let's use some local macros

\catcode`\@=11\relax

%% First of all, set up some delimiters
%% The units are in terms of em and ex, so if the font size is
%% changed, the units will be changed accordingly

%% To make things line up, we'll need the size of "Initial"

\font\m@ximaheaderfont=cmtt8  scaled 800
\setbox0=\hbox{\m@ximaheaderfont\ Initial}
\newdimen\in@tmaxima
\in@tmaxima = \wd0
\advance\in@tmaxima by 1em

\def\maximasm@llsk{\vskip 1ex}

\def\m@ximatop{
\vskip -1ex
\hbox to \hsize{\vrule depth 1ex  height .3pt width .4pt
                \vrule height .4pt depth 0pt width 1.1em 
                \lower .4ex \hbox{\maximaheadersize\ \Maxima} 
                \hrulefill 
                \vrule depth 1ex height .3pt width .4pt}
\vskip 1ex}


\def\m@ximainittop{
\vskip -1ex
\hbox to \hsize{\vrule depth 1ex  height .3pt width .4pt
                \vrule height .4pt depth 0pt width 1.1em 
                \lower .4ex \hbox{\maximaheadersize\ \Maxima} 
                \hrulefill
                \lower .4ex \hbox{\m@ximaheaderfont\ Initial}
                \vrule height .4pt depth 0pt width 1em 
                \vrule depth 1ex height .3pt width .4pt}
\vskip 1ex}

\def\m@ximaparttop#1{
\vskip -1ex
\hbox to \hsize{\vrule depth 1ex  height .3pt width .4pt
                \vrule height .4pt depth 0pt width 1.1em 
                \lower .4ex \hbox{\maximaheadersize\ \Maxima} 
                \hrulefill
                \lower .4ex \hbox{\m@ximaheaderfont\ #1}
                \hrulefill
                \vrule height .4pt depth 0pt width \in@tmaxima
                \vrule depth 1ex height .3pt width .4pt}
\vskip 1ex}

\def\m@ximainitparttop#1{
\vskip -1ex
\hbox to \hsize{\vrule depth 1ex  height .3pt width .4pt
                \vrule height .4pt depth 0pt width 1.1em 
                \lower .4ex \hbox{\maximaheadersize\ \Maxima} 
                \hrulefill
                \lower .4ex \hbox{\m@ximaheaderfont\ #1}
                \hrulefill
                \lower .4ex \hbox{\m@ximaheaderfont\ Initial}
                \vrule height .4pt depth 0pt width 1em
                \vrule depth 1ex height .3pt width .4pt}
\vskip 1ex}


\def\m@ximaoutput{
        \vskip -1ex
        \hbox to \hsize{\hskip 1.5em
                        \vrule height .4pt depth 0pt width 3em
                        \lower .4ex \hbox{\m@ximaheaderfont\ Output}
                        \hrulefill
                        \hskip 1.5em
                        }
        \vskip 1ex
}

\def\m@ximatexoutput{
        \vskip -1ex
        \hbox to \hsize{\hskip 1.5em
                        \vrule height .4pt depth 0pt width 3em
                        \lower .4ex \hbox{\m@ximaheaderfont\ \TeX\ Output}
                        \hrulefill
                        \hskip 1.5em
                        }
        \vskip 1ex
}

\def\m@ximabottom{
\hbox to \hsize{\vrule depth 0ex height 1ex width .4pt
                \hrulefill 
                \vrule depth 0ex height 1ex width .4pt}
}


%% Next, it is necessary to have a verbatim environment for the Maxima
%% input and output

%% The verbatim environment for the input is based mostly on the 
%% environment from "TeX for the Impatient" and Tim Morgan's verbtim2.tex

\def\maximadeactivate{%
        \catcode`\{ = 12
        \catcode`\} = 12
        \catcode`\$ = 12
        \catcode`\& = 12
        \catcode`\# = 12
        \catcode`\% = 12
        \catcode`\~ = 12
        \catcode`\^ = 12
        \catcode`\_ = 12
        \catcode`\  = 12
}

{\catcode`\^^M=13
 {\catcode`\ =13\gdef\maximaverbatimdefs{\def^^M{\ \par}\let =\ }}
 \gdef\verbatimgobble#1^^M{}}

\def\m@ximainputverbatim{
        \begingroup 
        \setlength{\parskip}{0pt}
        \par
        \maximainputfont
        \maximadeactivate
        \maximaverbatimdefs
        \catcode`\ =13 
        \catcode`\^^M=13
}


\let\endm@ximainputverbatim=\endgroup

%% The verbatim for Maxima output is taken from the TeXbook.  
%% It allows more symbols, but can't be used for the input
%% since the input can be ended with more than one delimiter.

\def\maximauncatcodespecials{\def\do##1{\catcode`##1=12 }\dospecials}

\chardef\active=13
{\catcode`\^^M=\active %
 \global\let^^M=\par} %

\def\setupm@ximaverbatim{\maximaoutputfont
\obeylines \maximauncatcodespecials \obeyspaces}
{\obeyspaces\global\let =\ }

\def\m@ximaoutputverbatim{\par\begingroup
        \baselineskip = \maximaoutputbaselineskip
        \setupm@ximaverbatim\dom@ximaverbatim}      
{\catcode`\|=0 \catcode`\\=12 %
 |obeylines|gdef|dom@ximaverbatim^^M#1\endmaxima{#1|endgroup|ifmaximalines|m@ximabottom|fi|par|vskip 1.5ex|par|endgroup}}

%% Now we need to set up the actual environments.
%% We will need some "if"s to tell us where we are

\newif\ifm@ximainput
\newif\ifm@ximaoutput
\newif\ifm@ximatexoutput

\def\maximanot@k{}

%% The top delimiter depends on whether or not it is an initial cell,
%% etc.

\def\beginmaxima{
     \begingroup
     \par  \vskip \premaximaspace \par
     \baselineskip = \maximainputbaselineskip
     \m@ximainputtrue
     \futurelet\maximanextchar\m@xima
}

\def\m@xima{
     \ifx\maximanextchar [%
         \let\maximanext = \m@ximainit
     \else
         \ifx\maximanextchar <%
              \let\maximanext = \m@ximapart
         \else
               \let\maximanext = \m@ximastart
         \fi
     \fi
     \maximanext
}

\def\m@ximainit[#1]{
     \futurelet\maximanextchar%
     \m@xima@nit}

\def\m@xima@nit{
     \ifx\maximanextchar <%
          \let\maximanext = \m@ximainitpart
     \else
          \let\maximanext = \m@ximainitonly
     \fi
     \maximanext
}

\def\m@ximainitonly{
     \par
     \vskip 1.5ex
     \par
     \ifmaximalines\m@ximainittop\fi
     \maximasm@llsk
     \m@ximainputverbatim
}


\def\m@ximastart{
     \par
     \vskip 1.5ex
     \par
     \ifmaximalines\m@ximatop\fi
     \maximasm@llsk
     \m@ximainputverbatim
}

\def\m@ximapart<#1:#2>{
     \par
     \vskip 1.5ex
     \par
     \def\maximatestt@k{#2}
     \ifx\maximatestt@k\maximanot@k
         \def\m@ximaparttext{Definition of package #1}
     \else
         \def\m@ximaparttext{<#1:#2>}
     \fi
     \ifmaximalines\m@ximaparttop\m@ximaparttext\fi
     \futurelet\maximanextchar\m@ximap@rt
}

\def\m@ximap@rt{
     \maximasm@llsk
     \m@ximainputverbatim
}

\def\m@ximainitpart<#1:#2>{
     \par
     \vskip 1.5ex
     \par
     \def\maximatestt@k{#2}
     \ifx\maximatestt@k\maximanot@k
         \def\m@ximaparttext{Definition of package #1}
     \else
         \def\m@ximaparttext{<#1:#2>}
     \fi
     \ifmaximalines\m@ximainitparttop\m@ximaparttext\fi
     \futurelet\maximanextchar\m@ximap@rt
}

%% Now to take care of the various types of output

\def\maximaoutput{
     \endm@ximainputverbatim
     \maximasm@llsk
     \m@ximainputfalse
     \m@ximaoutputtrue
     \ifmaximalines\m@ximaoutput\fi
%     \maximasm@llsk
     \m@ximaoutputverbatim
}      

\def\m@ximatexd#1\\{ \[ #1 \]\endgroup}
\def\m@ximatexe#1.#2\\{\[ E#1 = #2 \]\endgroup}


\def\maximatexoutput{
     \endm@ximainputverbatim
     \maximasm@llsk
     \m@ximainputfalse
     \m@ximatexoutputtrue
     \ifmaximalines\m@ximatexoutput\fi
     \maximasm@llsk
     \begingroup
     \def\D{\begingroup\par\m@ximatexd}
     \def\E{\begingroup\par\m@ximatexe}
     \def\p{\begingroup\setupm@ximasessionverbatim\dom@ximasessionpreprompt}
     \maximaoutputfont
}      

\def\@ndmaximatexoutput{
    \endgroup
    \m@ximatexoutputfalse
    \par
}

\def\endmaxima{
    \ifm@ximainput 
         \endm@ximainputverbatim
         \m@ximainputfalse
     \else
         \ifm@ximatexoutput
            \@ndmaximatexoutput
          \fi
      \fi
      \maximasm@llsk
      \ifmaximalines\m@ximabottom\fi
      \par
      \vskip \postmaximaspace
      \par
      \endgroup
}

%% Now the Maxima sessions

\def\maximasessionheadersize{\tiny}


%% First of all, set up some delimiters
%% The units are in terms of em and ex, so if the font size is
%% changed, the units will be changed accordingly

%% To make things line up, we'll need the size of "Initial"

\font\m@ximasessionheaderfont=cmtt8  scaled 800

\def\m@ximasessiontop{
\vskip -1ex
\hbox to \hsize{\vrule depth 1ex  height .3pt width .4pt
                \vrule height .4pt depth 0pt width 1.1em 
                \lower .4ex \hbox{\maximasessionheadersize\ \Maxima Session} 
                \hrulefill 
                \vrule depth 1ex height .3pt width .4pt}
\vskip 1ex}

\def\m@ximasessionbottom{
\hbox to \hsize{\vrule depth 0ex height 1ex width .4pt
                \hrulefill 
                \vrule depth 0ex height 1ex width .4pt}
}

\def\maximanothing{}

\def\m@ximasessioninputskip{
        \ifx \maximanexttok \maximasession
           \let \maximanext=\maximanothing
        \else
           \ifx \maximanexttok \maximatexsession
              \let \maximanext=\maximanothing
           \else     
              \ifx \maximanexttok \endmaximasession
                 \let \maximanext=\maximanothing
              \else
                  \let \maximanext=\m@ximasessioninputskiptwo
              \fi
           \fi
        \fi
        \maximanext}

\def\m@ximasessioninputskiptwo#1{\m@ximasessioninput}

\def\m@ximasessioninput{\obeylines\futurelet\maximanexttok\m@ximasessioninputskip}

\def\setupm@ximasessionverbatim{
\obeylines \maximauncatcodespecials \obeyspaces}
{\obeyspaces\global\let =\ }

\def\m@ximasessionsessionverbatim{\par
        \baselineskip = \maximasessionbaselineskip
        \setupm@ximasessionverbatim\dom@ximasessionverbatim}      
{\catcode`\|=0 \catcode`\\=12 %
 |obeylines|gdef|dom@ximasessionverbatim^^M#1\endmaximasession{#1|ifmaximasessionlines|m@ximasessionbottom|fi|par|vskip 1.5ex|par|endgroup}}

%% Now we need to set up the actual environments.
%% We will need some "if"s to tell us where we are

\newif\ifm@ximainsessioninput
\newif\ifm@ximasessionsession

%% The top delimiter depends on whether or not it is an initial cell,
%% etc.

\def\beginmaximasession{
     \begingroup
     \m@ximainsessioninputtrue
     \m@ximasessionsessionfalse
%     \m@ximatexsessionfalse
     \m@ximasessioninput
%     \futurelet\maximanexttok\m@ximasessioninputskip
}


%% Now to take care of the session

\def\maximasession{
      \endgroup
     \par  \vskip \premaximasessionspace \par
     \par
     \vskip 1.5ex
     \par
     \ifmaximasessionlines\m@ximasessiontop\fi
     \begingroup
     \baselineskip=\maximasessionbaselineskip
     \m@ximainsessioninputfalse
     \m@ximasessionsessiontrue
     \maximasessionoutputfont
     \m@ximasessionsessionverbatim
}      

\def\setupm@ximasessionverbatim{
\obeylines \maximauncatcodespecials \obeyspaces}
{\obeyspaces\global\let =\ }
{\catcode`\|=0 \catcode`\\=12 %
|obeylines|gdef|dom@ximatexsessionverbatim#1.#2\\{|par|textcolor{|maximapromptcolor}{(C#1)}#2|par|endgroup}
|obeylines|gdef|dom@ximasessionpreprompt#1\\{|par #1|par|endgroup}
}

\def\m@ximasessionoutput#1.#2\\{\textcolor{\maximapromptcolor}{(D#1)} \[ #2 \]\endgroup}

\def\m@ximasessioneoutput#1.#2\\{\textcolor{\maximapromptcolor}{(E#1)} \[ #2 \]\endgroup}

\def\maximatexsession{
      \endgroup
      \par  \vskip \premaximasessionspace \par
      \par
      \vskip 1.5ex
      \par
      \ifmaximasessionlines\m@ximasessiontop\fi
      \begingroup
      \m@ximainsessioninputfalse
      \m@ximasessionsessiontrue
      \def\C{\begingroup\color{\maximainputcolor}
        \setupm@ximasessionverbatim\dom@ximatexsessionverbatim}
      \def\D{\begingroup\color{\maximaoutputcolor}\par\m@ximasessionoutput}
      \def\E{\begingroup\color{\maximaoutputcolor}\par\m@ximasessioneoutput}
      \def\DD{\begingroup\color{\maximaoutputcolor}\par\m@ximatexd}
      \def\p{\begingroup\setupm@ximasessionverbatim\dom@ximasessionpreprompt}
      \baselineskip=\maximatexsessionbaselineskip
      \maximasessionoutputfont
}

\def\endmaximasession{
    \par
    \ifm@ximainsessioninput 
         \m@ximainsessioninputfalse
    \else
         \ifm@ximasessionsession
            \ifmaximasessionlines\m@ximasessionbottom\fi
            \par
            \vskip \postmaximasessionspace \par
            \par
            \maximasm@llsk             
         \fi
    \fi
    \color{black}
    \endgroup
}

%% Finally, the Maxima noshows.

\def\m@ximanoshowinputskip{
              \ifx \maximanexttok \endmaximanoshow
                 \let \maximanext=\maximanothing
              \else
                  \let \maximanext=\m@ximanoshowinputskiptwo
              \fi
        \maximanext}

\def\m@ximanoshowinputskiptwo#1{\m@ximanoshowinput}

\def\m@ximanoshowinput{\obeylines\futurelet\maximanexttok\m@ximanoshowinputskip}

\def\endm@ximanoshowinputskip{\endgroup}

\def\beginmaximanoshow{
     \begingroup
     \catcode`\%=11\relax
     \m@ximanoshowinput
}

\def\endmaximanoshow{
    \catcode`\%=14\relax
    \endgroup
}

\catcode`\@=12\relax

\endinput
